package com.haiyan.role.service.impl;

import com.haiyan.role.bean.RoleBean;
import com.haiyan.role.dao.RoleDao;
import com.haiyan.role.service.RoleService;
import com.haiyan.utils.DaoFactory;

import java.util.List;

/**
 * 这里要做的事情：实现service层的方法，但还是用dao层调用
 */
public class RoleServiceImpl implements RoleService {

    private RoleDao roleDao;

    {
        DaoFactory<RoleDao> daoFactory = new DaoFactory<>();
        roleDao=daoFactory.getDaoTransaction(RoleDao.class);
    }
    @Override
    public int addRole(String name) {
        RoleBean roleByName = roleDao.findRoleByName(name);
        if(roleByName==null){
           return roleDao.addRole(name);
        }
        return 0;
    }

    @Override
    public int deleteRoleById(Integer id) {
        int i= roleDao.deleteRoleById(id);
        return i;
    }

    @Override
    public int updateRoleNameById(Integer id, String name) {
        RoleBean roleByName = roleDao.findRoleByName(name);
        if(roleByName == null){
            return roleDao.updateRoleNameById(id, name);
        }
        return 0;
    }

    @Override
    public RoleBean findRoleNameById(Integer id) {
        RoleBean role = roleDao.findRoleNameById(id);
        return role;
    }

    @Override
    public RoleBean findRoleByName(String  name) {
        RoleBean roleByName = roleDao.findRoleByName(name);
        return roleByName;
    }


    @Override
    public List<RoleBean> findAllOrderByDateTime() {
        List<RoleBean> roleBeans = roleDao.findAllOrderByDateTime();
        return roleBeans;
    }
}
